// preprocessor.c
#include "Yecc.h"

void print_stacktrace() {
    void *buffer[64];
    int n = backtrace(buffer, 64);
    fprintf(stderr, "=== Stack trace (most recent call first) ===\n");
    backtrace_symbols_fd(buffer, n, fileno(stderr));
    fprintf(stderr, "=== End of stack trace ===\n");
}

char* preprocessor(const char* vanilla) {
    int len = Len(vanilla) + 1;
    char* src = (char*)malloc(len * sizeof(char));
    assert(src != NULL);

    int s = 0;
    int v = 0;
    while(true) {
        if(vanilla[v] == '\0') break;
        if(vanilla[v] == '#' || (vanilla[v] == '/' && vanilla[v+1] == '/')) {
            // remove "#include" and "//"
            while(vanilla[v] != '\n') v++;
            continue;
        }
        src[s] = vanilla[v];
        s++;
        v++;
    }

    src[s] = '\0';

    printf("\n### Preprocess Completed! ###\n\n");

    return src;
}
